﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Doo_JetDAL;
using DoodJetView.Core;

namespace DoodJetView.VuesPartielles
{
    /// <summary>
    /// Logique d'interaction pour EmployerForm_contrat.xaml
    /// </summary>
    public partial class EmployerForm_contrat : UserControl
    {
        private Employes.EtapesEmployes etapesEmployes;
        public long id_contret { get; set; }
       /* public EmployerForm_contrat(MainWindow mainWindow, EmployerForm_employe precedent)
        {
            InitializeComponent();
            comboBox_LoadForm();
            this.mainWindow = mainWindow;
            this.precedent = precedent;
            this.suivant = new EmployerForm_BrevetPermis(this.mainWindow, this);
        }

        public EmployerForm_contrat(EmployerForm_employe employerForm_employe)
        {
            // TODO: Complete member initialization
            this.employerForm_employe = employerForm_employe;
        }

        public EmployerForm_contrat(EmployerForm_BrevetPermis employerForm_BrevetPermis)
        {
            // TODO: Complete member initialization
            this.employerForm_BrevetPermis = employerForm_BrevetPermis;
        }

        public EmployerForm_contrat()
        {
            // TODO: Complete member initialization
        }

        public EmployerForm_contrat(MainWindow mainWindow_2, EmployerForm_BrevetPermis employerForm_BrevetPermis)
        {
            InitializeComponent();
            comboBox_LoadForm();
            // TODO: Complete member initialization
            this.mainWindow_2 = mainWindow_2;
            this.employerForm_BrevetPermis = employerForm_BrevetPermis;
        }*/

        public EmployerForm_contrat(Employes.EtapesEmployes etapesEmployes)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            comboBox_LoadForm();
            this.etapesEmployes = etapesEmployes;
        }

        private void comboBox_LoadForm()
        {
            using (DataEntities cont = new DataEntities())
            { 
                List<typeContrat> listeContrat = null;
           
                listeContrat = cont.typeContrat.ToList();
                foreach (typeContrat typeC in listeContrat)
                {
                    //comboBox_typeContrat.Items.Clear();
                    comboBox_typeContrat.Items.Add(new typeContrat_poco { id = (int)typeC.id_typeContrat, libelleContrat = typeC.libelle });
                }
                var i = 0;
                foreach (typeContrat_poco typeC in comboBox_typeContrat.Items)
                {
                    if (typeC.libelleContrat == "CDD")
                    {
                        comboBox_typeContrat.SelectedIndex = i;
                        labelDateDebutContrat.Visibility = Visibility.Visible;
                        calendarDateDebutContrat.Visibility = Visibility.Visible;
                        labelDateFinContrat.Visibility = Visibility.Visible;
                        calendarDateFinContrat.Visibility = Visibility.Visible;
                        btn_Suivant_contrat.Visibility = Visibility.Visible;
                        return;
                    }
                    i++;
                }
            }
            
        }

        private void btn_Submit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.SaveContrat();
                this.etapesEmployes.ContentUsers.Children.Clear();
                this.etapesEmployes.ContentUsers.Children.Add(
                        this.etapesEmployes.Etape3);
            }
            catch (Exception ex) { }
           
        }

        private void comboBox_typeContrat_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var itemSelectionne = comboBox_typeContrat.SelectedItem as typeContrat_poco;

            if (itemSelectionne.libelleContrat == "CDI")
            {
                labelDateDebutContrat.Visibility = Visibility.Visible;
                calendarDateDebutContrat.Visibility = Visibility.Visible;
                labelDateFinContrat.Visibility = Visibility.Hidden;
                calendarDateFinContrat.Visibility = Visibility.Hidden;
                btn_Suivant_contrat.Visibility = Visibility.Visible;
            }
            if (itemSelectionne.libelleContrat == "Intérim" || itemSelectionne.libelleContrat == "CDD")
            {
                labelDateDebutContrat.Visibility = Visibility.Visible;
                calendarDateDebutContrat.Visibility = Visibility.Visible;
                labelDateFinContrat.Visibility = Visibility.Visible;
                calendarDateFinContrat.Visibility = Visibility.Visible;
                btn_Suivant_contrat.Visibility = Visibility.Visible;
            }
        }

        public MainWindow mainWindow { get; set; }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.etapesEmployes.ContentUsers.Children.Clear();
            this.etapesEmployes.ContentUsers.Children.Add(
                    this.etapesEmployes.Etape1);
        }

        private void SaveContrat() {
            using (DataEntities db = new DataEntities())
            {
                contrat toAdd = new contrat
                {
                    id_typeContrat = ((typeContrat_poco)comboBox_typeContrat.SelectedItem).id,
                    debutContrat = DateTimeUnixTools.ConvertToUnixTimestamp((DateTime)calendarDateDebutContrat.SelectedDate),
                    finContrat = DateTimeUnixTools.ConvertToUnixTimestamp((DateTime)calendarDateFinContrat.SelectedDate)
                };
                db.contrat.AddObject(toAdd);
                db.SaveChanges();
                this.id_contret = toAdd.id_contrat;
            }
        
        }

    }
}
